<template>
	<view class="box">
		<view class="user" v-if="isLogin">
			<view>
				<image :src="avatarurl"></image>
			</view>
			<view>
				<text>{{username}}</text>
			</view>
			<view>
				<text>{{author==0?'管理员':'普通用户'}}</text>
			</view>
		</view>
		<view class="user" v-if="!isLogin">
			<view>
				<image src="../../static/user.jpg"></image>
			</view>
			<view>
				<text>请登录</text>
			</view>
			<view id="loginBox">
				<view @click="login">
					<text class="login">登录</text>
				</view>
			</view>
		</view>
		<view class="tab">
			<view class="title">
				<view class="title1" :class="index==0?'active':''" @click="()=>{index=0}">
					<image src="../../static/shoucang.png"></image>
					<text>我的收藏</text>
				</view>
				<view class="title2" :class="index==1?'active':''" @click="()=>{index=1}">
					<image src="../../static/shiwu.png"></image>
					<text>我的失物招领</text>
				</view>
				<view class="title3" :class="index==2?'active':''" @click="()=>{index=2}">
					<image src="../../static/xinxian.png"></image>
					<text>我的新鲜事</text>
				</view>
			</view>
			<view class="content" v-if="isLogin">
				<view class="content1" v-show="index==0">
					<view class="con" v-for="(item,index) in myFavorites" :key="index">
						<image :src="item.active_main_picture"></image>
						<view>
							<view>{{item.address}}</view>
							<view @click="()=>{
								go(item.active_id)
							}">报名</view>
						</view>
					</view>
				</view>
				<view class="content2" v-show="index==1">
					<view class="con2" v-for="(item,index) in myLastFound" :key="index">
						<view>
							<view>
								<image :src="item.avatarurl"></image>
								<view>{{item.username}}</view>
							</view>
							<view>{{dateStr2date(item.create_time)}}</view>
						</view>
						<view>{{item.last_found_title}}</view>
						<view>{{item.last_found_withtitle}}</view>
					</view>
				</view>
				<view class="content3" v-show="index==2">
					<view class="con3" v-for="(item,index) in myNewThing" :key="index">
						<view>
							<view>
								<image :src="item.avatarurl"></image>
								<view>{{item.username}}</view>
							</view>
							<view>{{dateStr2date(item.create_time).split(' ')[0]}}</view>
						</view>
						<view>{{item.title}}</view>
						<view class="imgBox">
							<view v-for="(item2,index2) in item.imagesList" :key="index2">
								<image :src="item2.img_url"></image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				index: 0,
				avatarurl: '',
				code: '',
				phone: '',
				username: '',
				isLogin: false,
				avatarurl:"../../static/touxiang.png",
				author:'1',
				id:-1,
				myFavorites:[],
				myLastFound:[],
				myNewThing:[]
			}
		},
		methods: {
			go(active_id){
				uni.navigateTo({
				    url: '../huodong/xiangqing?active_id='+active_id
				});
			},
			dateStr2date(t){
				let date=new Date(t).toLocaleString().replace(/:\d{1,2}$/,' ');
				return date;
			},
			login() {
				uni.login({
					provider: 'weixin',
					success: loginRes => {
						this.code = loginRes.code;
						this.$http({
							url: '/user/onlogin',
							method: 'GET',
							data: {
								code: this.code,
								password: 222222,
								phone: 18822222222
							}
						}).then(res=>{
							if(res.data.code==200){
								this.isLogin=true;
								uni.setStorageSync('wx_user_id', res.data.data.id);
								// this.id=res.data.data.id;
								this.id=1;
								this.avatarurl=res.data.data.avatarurl;
								this.username=res.data.data.username;
								uni.setStorageSync('wx_user_author', res.data.data.author);
								this.author=res.data.data.author;
								this.$http({
									url: '/user/myFavorites',
									method: 'GET',
									data: {
										// wx_user_id:this.id
										wx_user_id:1
									}
								}).then(res=>{
									this.myFavorites=res.data.data
								})
								this.$http({
									url: '/user/myLastFound',
									method: 'GET',
									data:{
										// wx_user_id:this.id
										wx_user_id:1
									}
								}).then(res=>{
									this.myLastFound=res.data.data
								})
								this.$http({
									url: '/user/myNewThing',
									method: 'GET',
									data:{
										// wx_user_id:this.id
										wx_user_id:1
									}
								}).then(res=>{
									this.myNewThing=res.data.data
									this.$http({
										url:'/newthing/findAllNewThing',
										method: 'GET'
									}).then(res=>{
										res.data.data.forEach(v=>{
											if(v.wx_user_id==this.id){
												this.myNewThing.forEach(v2=>{
													if(v.id==v2.id){
														v2.imagesList=v.imagesList
													}
												})
											}
										})
									})
								})
							}
						})
					}
				});
			}
		},
		onLoad() {
			if(uni.getStorageSync('wx_user_id')&&uni.getStorageSync('wx_user_id')==6)this.login()
		}
	}
</script>

<style lang="less" scoped>
	.user {
		width: 750rpx;
		height: 440rpx;
		background: rgb(249, 204, 102);
	}

	.title {
		display: flex;
		justify-content: space-around;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		font-size: 24rpx;

		.title1 {
			width: 160rpx;
		}

		.title2 {
			width: 220rpx;
		}

		.title3 {
			width: 160rpx;
		}

		image {
			width: 24rpx;
			height: 24rpx;
			margin-right: 16rpx;
		}

		.active {
			border-bottom: 5rpx solid rgb(249, 204, 102);
		}
	}

	.user {
		display: flex;
		flex-direction: column;

		>view {
			display: flex;
			justify-content: center;
			margin-top: 30rpx;
		}

		>view:first-of-type {
			margin-top: 90rpx;
		}

		image {
			width: 120rpx;
			height: 120rpx;
			border-radius: 50%;
		}

		>view:nth-of-type(2) {
			font-weight: 600;
		}

		>view:nth-of-type(3) {
			font-size: 24rpx;
			border: 1rpx solid rgb(23, 23, 23);
			width: 138rpx;
			height: 50rpx;
			margin: 30rpx auto 0;
			line-height: 50rpx;
			border-radius: 8rpx;
		}

		#loginBox {
			width: 350rpx;
			text-align: center;
			border: none;
			display: flex;
			justify-content: space-around;

			>view {
				font-size: 24rpx;
				border: 1rpx solid rgb(23, 23, 23);
				width: 138rpx;
				height: 50rpx;
				line-height: 50rpx;
				border-radius: 8rpx;
			}
		}

	}

	.content {
		margin: 30rpx auto 0;
		width: 690rpx;

		.content1 {
			margin: 0 auto;

			.con {
				margin-bottom: 60rpx;
				width: 690rpx;
				display: flex;
				flex-direction: column;
			}

			image {
				width: 690rpx;
				height: 300rpx;
				border-radius: 19rpx 19rpx 0 0;
			}

			>view>view {
				flex: 1;
				padding: 0 30rpx;
				margin-top: 30rpx;
				height: 60rpx;
				display: flex;
				justify-content: space-between;

				view:first-of-type {
					font-size: 32rpx;
					color: rgb(37, 37, 37);
					font-weight: 600;
				}

				view:last-of-type {
					width: 165rpx;
					height: 60rpx;
					border-radius: 19rpx;
					background: rgb(254, 204, 102);
					font-size: 28rpx;
					color: rgb(37, 37, 37);
					line-height: 60rpx;
					text-align: center;
				}
			}
		}
	}

	.con2 {
		margin-top: 60rpx;

		>view:first-of-type {
			display: flex;
			justify-content: space-between;

			image {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
				margin-right: 20rpx;
			}

			>view {
				height: 80rpx;
				line-height: 80rpx;
				display: flex;
				font-size: 24rpx;
				color: rgb(180, 180, 180);

				>view {
					font-weight: 600;
					font-size: 32rpx;
					color: #000;
				}
			}
		}

		>view:nth-of-type(2) {
			margin-left: 96rpx;
			font-weight: 600;
		}

		>view:nth-of-type(3) {
			margin-left: 96rpx;
			margin-top: 30rpx;
			font-size: 24rpx;
			color: rgb(113, 113, 113);
		}
	}

	.con3 {
		margin-top: 60rpx;

		>view:first-of-type {
			display: flex;
			justify-content: space-between;

			image {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
				margin-right: 20rpx;
			}

			>view {
				height: 80rpx;
				line-height: 80rpx;
				display: flex;
				font-size: 24rpx;
				color: rgb(180, 180, 180);

				>view {
					font-weight: 600;
					font-size: 32rpx;
					color: #000;
				}
			}
		}

		>view:nth-of-type(2) {
			margin-left: 96rpx;
			margin-bottom: 30rpx;
			font-size: 24rpx;
			color: rgb(113, 113, 113);
		}

		.imgBox {
			margin-left: 96rpx;

			image {
				width: 190rpx;
				height: 190rpx;
				border-radius: 19rpx;
				margin-right: 10rpx;
			}

			image:nth-of-type(3n) {
				margin-right: 0;
			}
		}
	}
</style>
